<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SSL证书生成工具</title>
    <!-- 添加必要的加密库 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/forge/0.10.0/forge.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 20px auto;
            padding: 0 20px;
        }
        .container {
            background-color: #f5f5f5;
            padding: 20px;
            border-radius: 5px;
        }
        .warning {
            background-color: #ffebee;
            border: 1px solid #ffcdd2;
            padding: 10px;
            margin-bottom: 20px;
            border-radius: 4px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        textarea {
            width: 100%;
            height: 200px;
            margin: 10px 0;
            padding: 10px;
            box-sizing: border-box;
            font-family: monospace;
        }
        .btn {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        .btn:hover {
            background-color: #45a049;
        }
        .copy-btn {
            background-color: #2196F3;
        }
        .copy-btn:hover {
            background-color: #1976D2;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>SSL证书生成工具</h2>
        
        <div class="warning">
            注意：此工具生成的是自签名证书，仅用于测试目的。生产环境请使用受信任的 CA 机构颁发的证书。
        </div>

        <div class="form-group">
            <label for="commonName">域名 (Common Name):</label>
            <input type="text" id="commonName" placeholder="例如: example.com" required>
        </div>

        <div class="form-group">
            <label for="organization">组织名称 (Organization):</label>
            <input type="text" id="organization" placeholder="例如: My Company Ltd">
        </div>

        <div class="form-group">
            <label for="validDays">有效期（天）:</label>
            <input type="number" id="validDays" value="365">
        </div>

        <button class="btn" onclick="generateCertificate()">生成证书</button>
        
        <h3>私钥 (Private Key)</h3>
        <div style="position: relative;">
            <textarea id="privateKey" readonly></textarea>
            <button class="btn copy-btn" onclick="copyToClipboard('privateKey')" style="position: absolute; right: 10px; top: 10px;">复制</button>
        </div>

        <h3>证书 (Certificate)</h3>
        <div style="position: relative;">
            <textarea id="certificate" readonly></textarea>
            <button class="btn copy-btn" onclick="copyToClipboard('certificate')" style="position: absolute; right: 10px; top: 10px;">复制</button>
        </div>

        <button class="btn" onclick="downloadFiles()">下载证书文件</button>
    </div>

    <script>
        async function generateCertificate() {
            const commonName = document.getElementById('commonName').value;
            if (!commonName) {
                alert('请输入域名！');
                return;
            }

            const organization = document.getElementById('organization').value;
            const validDays = parseInt(document.getElementById('validDays').value);

            // 生成RSA密钥对
            const keypair = forge.pki.rsa.generateKeyPair({ bits: 2048 });

            // 创建证书
            const cert = forge.pki.createCertificate();
            cert.publicKey = keypair.publicKey;
            cert.serialNumber = '01';
            cert.validity.notBefore = new Date();
            cert.validity.notAfter = new Date();
            cert.validity.notAfter.setDate(cert.validity.notBefore.getDate() + validDays);

            const attrs = [{
                name: 'commonName',
                value: commonName
            }, {
                name: 'organizationName',
                value: organization || commonName
            }];

            cert.setSubject(attrs);
            cert.setIssuer(attrs);

            // 设置扩展
            cert.setExtensions([{
                name: 'basicConstraints',
                cA: true
            }, {
                name: 'keyUsage',
                keyCertSign: true,
                digitalSignature: true,
                nonRepudiation: true,
                keyEncipherment: true,
                dataEncipherment: true
            }, {
                name: 'subjectAltName',
                altNames: [{
                    type: 2, // DNS
                    value: commonName
                }]
            }]);

            // 使用私钥签名证书
            cert.sign(keypair.privateKey, forge.md.sha256.create());

            // 转换为PEM格式
            const privateKeyPem = forge.pki.privateKeyToPem(keypair.privateKey);
            const certificatePem = forge.pki.certificateToPem(cert);

            document.getElementById('privateKey').value = privateKeyPem;
            document.getElementById('certificate').value = certificatePem;
        }

        function copyToClipboard(elementId) {
            const element = document.getElementById(elementId);
            element.select();
            document.execCommand('copy');
            alert('已复制到剪贴板！');
        }

        function downloadFiles() {
            const privateKey = document.getElementById('privateKey').value;
            const certificate = document.getElementById('certificate').value;
            
            if (!privateKey || !certificate) {
                alert('请先生成证书！');
                return;
            }

            // 下载私钥文件
            downloadFile('private.key', privateKey);
            // 下载证书文件
            downloadFile('certificate.pem', certificate);
        }

        function downloadFile(filename, content) {
            const element = document.createElement('a');
            element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(content));
            element.setAttribute('download', filename);
            element.style.display = 'none';
            document.body.appendChild(element);
            element.click();
            document.body.removeChild(element);
        }
    </script>
</body>
</html>